package com.qianfeng.xinyihezi.xinyihezi.jsondecode;

import com.qianfeng.xinyihezi.xinyihezi.model.GoodsData;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Tcoy on 2016/11/15.
 */

public class DecodeGoodsBase {
    private List<GoodsData> goodsDataList;

    public DecodeGoodsBase() {
        goodsDataList = new ArrayList<>();
    }

    public List<GoodsData> decodeGoodsBase(String json) {
        try {
            JSONObject jsonObjectAll = new JSONObject(json);
            JSONArray jsonArrayData = jsonObjectAll.optJSONArray("data");
            if (jsonObjectAll != null && jsonArrayData != null) {
                for (int i = 0; i < jsonArrayData.length(); i++) {
                    JSONObject jsonObjectGroup = (JSONObject) jsonArrayData.get(i);
                    GoodsData goodsData = new GoodsData(jsonObjectGroup.optString("freight"), jsonObjectGroup.optString("goods_id"),
                            jsonObjectGroup.optString("html_src"), jsonObjectGroup.optString("image_url"), jsonObjectGroup.optString("like_count"),
                            jsonObjectGroup.optString("logo_image"), jsonObjectGroup.optString("p_price"), jsonObjectGroup.optString("price"),
                            jsonObjectGroup.optString("product_id"), jsonObjectGroup.optString("sale_count"), jsonObjectGroup.optString("share_url"),
                            jsonObjectGroup.optString("short_description"), jsonObjectGroup.optString("short_name"), jsonObjectGroup.optString("special_price_tag"),
                            jsonObjectGroup.optString("store"), jsonObjectGroup.optString("store_name"), jsonObjectGroup.optString("type_id"));
                    goodsDataList.add(goodsData);
                }
            }
            return goodsDataList;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }
}
